/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <memory>
#include <mutex>

#include "ability_info.h"
#include "event_handler.h"
#include "iremote_object.h"
#include "want.h"

#ifndef OHOS_ABILITY_APP_RECOVERY_H
#define OHOS_ABILITY_APP_RECOVERY_H

namespace OHOS {
namespace AbilityRuntime {
class Ability;
enum RestartFlag {
    ALWAYS_RESTART,
    CPP_CRASH_NO_RESTART,
    JS_CRASH_NO_RESTART,
    APP_FREEZE_NO_RESTART,
};

enum SaveFlag {
    NO_SAVE,
    SAVE_WHEN_ERROR,
    SAVE_WHEN_BACKGROUND,
};

enum SaveMode {
    SAVE_WITH_FILE,
    SAVE_WITH_DB,
    SAVE_WITH_SHARED_MEMORY,
};
class AppRecovery {
public:
    AppRecovery();
    ~AppRecovery();
    static AppRecovery& GetInstance();
    void EnableAppRecovery(RestartFlag restartFlag, SaveFlag saveFlag, SaveMode saveMode);
    bool InitApplicationInfo(const std::shared_ptr<Ability> &ability, const std::shared_ptr<AbilityInfo> &abilityInfo);
    bool IsEnabled() const;
    bool SaveAbilityState(int32_t savedStateId);
    bool SerializeDataToFile(int32_t savedStateId, WantParams& params);
    bool ReadSerializeDataFromFile(int32_t savedStateId, WantParams& params);
    RestartFlag GetRestartFlag() const;
    SaveFlag GetSaveFlag() const;
    SaveMode GetSaveMode() const;

private:
    bool isEnable_;
    RestartFlag restartFlag_;
    SaveFlag saveFlag_;
    SaveMode saveMode_;
    std::weak_ptr<Ability> ability_;
    std::weak_ptr<AbilityInfo> abilityInfo_;
    std::shared_ptr<EventHandler> mainHandler_ = nullptr;
    std::mutex lock_;
};
}  // namespace AbilityRuntime
}  // namespace OHOS
#endif // OHOS_ABILITY_APP_RECOVERY_H
